//
//  MainCollectionViewCell.m
//  XiMaLaYa
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 L-G. All rights reserved.
//

#import "MainCollectionViewCell.h"
#import "EditorInforCollectionViewCell.h"
#import "TitleCollectionReusableView.h"
#import "SDCycleScrollView.h"
#import "GetConnection.h"
#import "SortModel.h"
#import "FocusImages.h"

#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375

@implementation MainCollectionViewCell
- (void)dealloc
{
    [_arrayData release];
    [_arrayTitle release];
    [_arrayLoopsView release];
    [_collectionView release];
    [super dealloc];
}
#pragma mark 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.arrayData = [NSMutableArray array];
        self.arrayTitle = [NSMutableArray array];
        self.arrayLoopsView = [NSMutableArray array];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        
        self.collectionView.showsVerticalScrollIndicator = NO;
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView setContentInset:UIEdgeInsetsMake(180, 0, 0, 0)];
        [_collectionView release];
        
        // 注册cell
        [self.collectionView registerClass:[EditorInforCollectionViewCell class] forCellWithReuseIdentifier:@"editorCell"];
        
        // 注册头视图
        [self.collectionView registerClass:[TitleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReusable"];
        
        // 注册尾视图
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReusable"];
        
        // 请求数据
        [self getconnection];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
#pragma mark collectionView返回的各种属性
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * HEIGHT;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5 * HEIGHT;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 * WIDTH, 10 * HEIGHT, 10 * WIDTH, 10 * HEIGHT);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(110 * WIDTH, 140 * HEIGHT);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.arrayData[section];
   return array.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.arrayData.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        TitleCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReusable" forIndexPath:indexPath];
        header.sortModel = self.arrayTitle[indexPath.section];
        return header;
    }else
    {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReusable" forIndexPath:indexPath];
        footer.backgroundColor = [UIColor groupTableViewBackgroundColor];
        return footer;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.frame.size.width, 20 * HEIGHT);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(self.frame.size.width, 8 * HEIGHT);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EditorInforCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"editorCell" forIndexPath:indexPath];
    EditorAlbums *editorAlbums = [[self.arrayData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.editorAlbums = editorAlbums;
    return cell;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.delegate sendDelegate:1];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.delegate sendDelegate:0];
}
- (void)getconnection
{
    NSString *str = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=ios-b1&device=iPhone&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20";
    
    [GetConnection startGETConnection:str parmaters:nil connectionBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *hotRecommend = [[dic objectForKey:@"hotRecommends"] objectForKey:@"list"];
        NSArray *focusI = [[dic objectForKey:@"focusImages"] objectForKey:@"list"];
        
        // 取出数据内容
        for (NSDictionary *temp in hotRecommend) {
            SortModel *sortModel = [SortModel sortModelWithDictionary:temp];
            [self.arrayTitle addObject:sortModel];
            // 初始化数组
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dic in sortModel.list) {
                EditorAlbums *editorAlbums = [[EditorAlbums alloc] init];
                
                [editorAlbums setValuesForKeysWithDictionary:dic];
                
                [array addObject:editorAlbums];
            }
            [self.arrayData addObject:array];
        }
        // 取出数据中的轮播图
        NSMutableArray *arrayPicUrl = [NSMutableArray array];
        for (NSDictionary *temp in focusI) {
            if ([temp.allKeys containsObject:@"uid"]) {
                FocusImages *focusImages = [[FocusImages alloc] init];
                [focusImages setValuesForKeysWithDictionary:temp];
                [arrayPicUrl addObject:focusImages.pic];
                [self.arrayLoopsView addObject:focusImages];
            }
        }
        // 创建轮播图
        SDCycleScrollView *LoopsView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, - 180, self.frame.size.width, 180 * HEIGHT) imageURLStringsGroup:arrayPicUrl];
        [self.collectionView addSubview:LoopsView];
        
        [self.collectionView reloadData];
    }];
}
@end
